package com.androidx.p013lv.base.utils;

import com.androidx.p013lv.base.BaseApp;
import com.androidx.p013lv.base.bean.VideoHistory;
import com.androidx.p013lv.base.p014db.VideoHistoryDao;
import java.util.List;
import org.greenrobot.greendao.query.WhereCondition;
import p023io.reactivex.BackpressureStrategy;
import p023io.reactivex.Completable;
import p023io.reactivex.CompletableEmitter;
import p023io.reactivex.CompletableOnSubscribe;
import p023io.reactivex.Flowable;
import p023io.reactivex.FlowableEmitter;
import p023io.reactivex.FlowableOnSubscribe;

/* renamed from: com.androidx.lv.base.utils.VideoHistoryUtils */
public class VideoHistoryUtils {
    static VideoHistoryUtils videoHistoryUtils;

    public static VideoHistoryUtils getInstance() {
        if (videoHistoryUtils == null) {
            videoHistoryUtils = new VideoHistoryUtils();
        }
        return videoHistoryUtils;
    }

    public VideoHistoryDao getDao() {
        return BaseApp.getDaoSession().getVideoHistoryDao();
    }

    public Completable insert(final VideoHistory videoHistory) {
        return Completable.create(new CompletableOnSubscribe() {
            /* class com.androidx.p013lv.base.utils.VideoHistoryUtils.C10641 */

            @Override // p023io.reactivex.CompletableOnSubscribe
            public void subscribe(CompletableEmitter completableEmitter) throws Exception {
                VideoHistoryDao dao = VideoHistoryUtils.this.getDao();
                List list = dao.queryBuilder().where(VideoHistoryDao.Properties.VideoId.mo36808eq(Integer.valueOf(videoHistory.getVideoId())), new WhereCondition[0]).build().list();
                VideoHistory videoHistory = (list == null || list.size() <= 0) ? null : (VideoHistory) list.get(0);
                if (videoHistory != null) {
                    dao.delete(videoHistory);
                }
                dao.insert(videoHistory);
                completableEmitter.onComplete();
            }
        });
    }

    public Completable delete(final List<VideoHistory> list) {
        return Completable.create(new CompletableOnSubscribe() {
            /* class com.androidx.p013lv.base.utils.VideoHistoryUtils.C10652 */

            @Override // p023io.reactivex.CompletableOnSubscribe
            public void subscribe(CompletableEmitter completableEmitter) throws Exception {
                VideoHistoryUtils.this.getDao().deleteInTx(list);
                completableEmitter.onComplete();
            }
        });
    }

    public Completable delete(final VideoHistory videoHistory) {
        return Completable.create(new CompletableOnSubscribe() {
            /* class com.androidx.p013lv.base.utils.VideoHistoryUtils.C10663 */

            @Override // p023io.reactivex.CompletableOnSubscribe
            public void subscribe(CompletableEmitter completableEmitter) throws Exception {
                VideoHistoryUtils.this.getDao().delete(videoHistory);
                completableEmitter.onComplete();
            }
        });
    }

    public Flowable<List<VideoHistory>> select(final Long l) {
        return Flowable.create(new FlowableOnSubscribe<List<VideoHistory>>() {
            /* class com.androidx.p013lv.base.utils.VideoHistoryUtils.C10674 */

            @Override // p023io.reactivex.FlowableOnSubscribe
            public void subscribe(FlowableEmitter<List<VideoHistory>> flowableEmitter) throws Exception {
                List<VideoHistory> list;
                if (l.longValue() == 0) {
                    list = VideoHistoryUtils.this.getDao().queryBuilder().orderDesc(VideoHistoryDao.Properties.f179Id).limit(30).build().list();
                } else {
                    list = VideoHistoryUtils.this.getDao().queryBuilder().where(VideoHistoryDao.Properties.f179Id.mo36817lt(l), new WhereCondition[0]).orderDesc(VideoHistoryDao.Properties.f179Id).limit(30).build().list();
                }
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setEditState(0);
                }
                flowableEmitter.onNext(list);
            }
        }, BackpressureStrategy.ERROR);
    }
}
